<?php
$path = dirname(__FILE__);
chdir($path);
require_once '../Common/common.php';

class Demo
{
	public function start()
	{
		$MyRedis = new MyRedis();
		$MtCronModel = new MtCronModel();

		$key = sprintf("KCS_QUEUE_%s_SYNC_PRODUCT_STOCK", "MEITUAN");

		global $SyncProductStockLimit; //同步库存数量限制
		$data = array();

		while(true){
			for($i = 0; $i < $SyncProductStockLimit; $i++){
				$value = $MyRedis->LPOP($key);
				
				if(empty($value)){
					continue;
				}

				$value = explode("_", $value);

				$store_id = $value[0];
				$other_product_id = $value[1];
				$product_stock = $value[2];

				$data[] = array(
					"store_id" => $store_id,
					"other_product_id" => $other_product_id,
					"product_stock" => $product_stock
				);
			}
			
			if(empty($data)){
				$data = array();
				continue;
			}

			//批量同步库存的时候 里面存在多个商户id  所以只针对平台限制
			$req = $MtCronModel->getSyncProductLimit("MEITUAN", "STOCK");

			if($req){
				$MtCronModel->syncStock($data);
			}else{
				foreach ($data as $key => $value) {
					$MyRedis->RPUSH($key, implode("_", $value));
				}
				
			}
		}
	}
}

$Demo = new Demo();
$Demo->start();

?>